package com.itlong.cloud.smartcard.service.impl;

import com.itlong.cloud.POJO.DTO.smartcard.SmartCardOldGetDeviceRecordDTO;
import com.itlong.cloud.POJO.VO.smartcard.SmartCardOldEquipRecordVO;
import com.itlong.cloud.enumerate.PropertyEquipTypeEnum;
import com.itlong.cloud.smartcard.dao.ISmartCardOldEquipRecordDao;
import com.itlong.cloud.smartcard.service.ISmartCardOldEquipRecordService;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      旧一卡通_设备通行记录接口实现。
 * <desc/>
 *
 * @createDate 2017-12-19.
 */
@Service
public class SmartCardOldEquipRecordServiceImpl implements ISmartCardOldEquipRecordService{

    //旧一卡通通行记录接口
    @Autowired
    ISmartCardOldEquipRecordDao iSmartCardOldEquipRecordDao;
    /**
     * <desc>
     *      获取通行记录。
     * <desc/>
     *
     * @param pageDTO
     * @return
     * @author shengen.T
     * @createDate 2017-12-19
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Page<SmartCardOldEquipRecordVO> getEquipRecordPage(SmartCardOldGetDeviceRecordDTO pageDTO) throws Exception{
        Integer deviceType = StringHandlerUtil.strToIntDef(pageDTO.getDeviceType(),-1);
        Map<String,Object> params = new HashMap<>();
        //分页类默认pageSize为20
        Page<SmartCardOldEquipRecordVO> page = new Page<>();
        page.getPage().setPageSize(pageDTO.getPageSize());
        page.getPage().setCurrentPage(pageDTO.getPageNo());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        params.put("projectId",pageDTO.getProjectId());
        List<SmartCardOldEquipRecordVO> recordList=null;
        Integer recordCount=0;
        //电梯通行记录
        if(deviceType== PropertyEquipTypeEnum.ELEVATOR.getType()){
            recordList = iSmartCardOldEquipRecordDao.getElevatorRecordPage(params);
            recordCount = iSmartCardOldEquipRecordDao.getElevatorPageCount(params);

        }else if(deviceType==PropertyEquipTypeEnum.ENTRANCE.getType()){   //门禁通行记录
            recordList = iSmartCardOldEquipRecordDao.getEntranceRecordPage(params);
            recordCount = iSmartCardOldEquipRecordDao.getEntrancePageCount(params);
        }
        if(recordList!=null&&recordList.size()>0){
            List<String> idList = new ArrayList<>();
            for (SmartCardOldEquipRecordVO vo:recordList){
                idList.add(vo.getId());
                vo.setPassTime(Double.valueOf(vo.getPassTimeStr()));
            }
            if(deviceType== PropertyEquipTypeEnum.ELEVATOR.getType()){
                 iSmartCardOldEquipRecordDao.updateEleCheckTimeBatch(idList);
            }else if(deviceType==PropertyEquipTypeEnum.ENTRANCE.getType()){
                iSmartCardOldEquipRecordDao.updateEntCheckTimeBatch(idList);
            }
        }
        page.setRows(recordList,recordCount);
        return page;
    }
}
